package com.skiving.cloud.admin.service.impl;

import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.math.Calculator;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.admin.entity.SysCaptcha;
import com.skiving.cloud.admin.mapper.SysCaptchaMapper;
import com.skiving.cloud.admin.service.SysCaptchaService;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.cache.CacheService;

import java.awt.image.BufferedImage;
import java.time.LocalDateTime;

/**
 * 系统验证码 服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-10
 */
@Component
public class SysCaptchaServiceImpl extends ServiceImpl<SysCaptchaMapper, SysCaptcha>  implements SysCaptchaService {
    public static final String CAPTCHA_REDIS_KEY_PREFIX = "adminCaptcha:";
    @Inject
    private CacheService cache;
    /**
     * 分页查询系统验证码
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysCaptcha> queryByPage(PageParameter<SysCaptcha> parameter){
        Page<SysCaptcha> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getUuid())) {
                queryWrapper.and(SysCaptcha::getUuid).like(parameter.getParam().getUuid());
            }
            if (Utils.isNotBlank(parameter.getParam().getCode())) {
                queryWrapper.and(SysCaptcha::getCode).like(parameter.getParam().getCode());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 获取图片验证码
     *
     * @param uuid uuid
     */
    @Override
    public BufferedImage getCaptcha(String uuid) throws Exception {
        if(Utils.isBlank(uuid)){
            throw new Exception("uuid不能为空");
        }
        ShearCaptcha captchaShear = new ShearCaptcha(200, 60, 6, 2);
        captchaShear.setGenerator(new MathGenerator(1));
        captchaShear.createCode();
        int calculateResult = (int) Calculator.conversion(captchaShear.getCode());
        String code = calculateResult + "";
        SysCaptcha captcha = new SysCaptcha();
        captcha.setUuid(uuid);
        captcha.setCode(code);
        //5分钟后过期
        captcha.setExpireTime(LocalDateTime.now().plusMinutes(5));
        cache.store(CAPTCHA_REDIS_KEY_PREFIX + uuid, captcha, 300);
        return captchaShear.getImage();
    }

    /**
     * 验证码效验
     *
     * @param uuid uuid
     * @param code 验证码
     * @return true：成功  false：失败
     */
    @Override
    public boolean validate(String uuid, String code) {
        SysCaptcha sysCaptcha = cache.get(CAPTCHA_REDIS_KEY_PREFIX + uuid, SysCaptcha.class);
        if(sysCaptcha == null){
            return false;
        }
        cache.remove(CAPTCHA_REDIS_KEY_PREFIX + uuid);
        return sysCaptcha.getCode().equalsIgnoreCase(code) && sysCaptcha.getExpireTime().isAfter(LocalDateTime.now());
    }
}
